<?php

/**
 * @author Laurent SITTLER <lolo.sittler@gmail.com>
 * @author Alexandre GOMEZ <gomez.alexandre02@gmail.com>
 * @version 0.1
 */
require_once 'Library/External_Database.php';
require_once 'Database_Model.php';

/**
 * Requete Model Class
 */
class Requete_Model extends Model {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
    }

    function External() {
        parent::External();
    }

    /**
     * Set user request
     */
    public function SetRequest($req) {
        $result = NULL;
        if (Session::Get(Constants::$SESSION_cdb) != NULL) {
            $rdb = new Database_Model();
            $cdb = $rdb->GetById(Session::Get(Constants::$SESSION_cdb));
            $cdb->SetLogin(Session::Get(Constants::$SESSION_ldb));
            $cdb->SetPassword(Session::Get(Constants::$SESSION_pdb));
            $bdd = new External_Database($cdb);
            if (Session::Get(Constants::$SESSION_error) == NULL)
                $result = $bdd->prepare($req);
        } else {
            $result = $this->db->prepare($req, array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));
        }
        // S'il y a une erreur de connexion à la base
        if (Session::Get(Constants::$SESSION_error) == NULL) {
            $result->setFetchMode(PDO::FETCH_ASSOC);
            $result->execute();

            $chkError = $result->errorInfo();
            if ($chkError[2] != NULL) {
                Session::Set(Constants::$SESSION_error, $chkError[2]);
                return NULL;
            } else {
                $test = $result->fetchAll();
                return $test;
            }
        }
        else
            return NULL;
        $result->closeCursor();
    }

}
